package com.cat.dataStructure03;

import java.io.*;
import java.util.Arrays;
import java.util.Map;

/**
 * @author 曲大人的喵
 * @description https://www.luogu.com.cn/problem/P3379
 * @create 2025/11/20 11:37
 * @since JDK17
 */

public class Solution92 {
    static int MAXN = 500002, n, m, s, tcnt, qcnt;
    static int[] headTree = new int[MAXN], nextEdge = new int[MAXN << 1], toEdge = new int[MAXN << 1], query = new int[MAXN << 1];
    static int[] f = new int[MAXN], headQuery = new int[MAXN], nextQuery = new int[MAXN << 1], toQuery = new int[MAXN << 1];
    static int[] ans = new int[MAXN];
    static boolean[] vis = new boolean[MAXN];
    static void build() {
        qcnt = tcnt = 1;
        Arrays.fill(headTree, 1, n + 1, 0);
        Arrays.fill(headQuery, 1, n + 1, 0);
        Arrays.fill(vis, 1, n + 1, false);
        for (int i = 1; i <= n; i++) {
            f[i] = i;
        }
    }
    static void addEdge(int u, int v) {
        nextEdge[tcnt] = headTree[u];
        toEdge[tcnt] = v;
        headTree[u] = tcnt++;
    }
    static void addQuery(int u, int v, int i) {
        nextQuery[qcnt] = headQuery[u];
        toQuery[qcnt] = v;
        headQuery[u] = qcnt;
        query[qcnt++] = i;
    }
    static int find(int x) {
        if (x != f[x]) {
            f[x] = find(f[x]);
        }
        return f[x];
    }
    static void tarjan(int u, int fa) {
        vis[u] = true;
        for (int e = headTree[u]; e != 0; e = nextEdge[e]) {
            if (toEdge[e] != fa) {
                tarjan(toEdge[e], u);
                f[toEdge[e]] = u;
            }
        }

        for (int e = headQuery[u]; e != 0; e = nextQuery[e]) {
            if (vis[toQuery[e]]) {
                ans[query[e]] = find(toQuery[e]);
            }
        }
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        in.nextToken();
        n = ((int) in.nval);
        in.nextToken();
        m = ((int) in.nval);
        in.nextToken();
        s = ((int) in.nval);
        build();
        for (int i = 1, x, y; i < n; i++) {
            in.nextToken();
            x = ((int) in.nval);
            in.nextToken();
            y = ((int) in.nval);
            addEdge(x, y);
            addEdge(y, x);
        }
        for (int i = 0, x, y; i < m; i++) {
            in.nextToken();
            x = ((int) in.nval);
            in.nextToken();
            y = ((int) in.nval);
            addQuery(x, y, i);
            addQuery(y, x, i);
        }
        tarjan(s, 0);
        for (int i = 0; i < m; i++) {
            out.println(ans[i]);
        }

        out.flush();
        out.close();
        br.close();
    }
}
